from django.db import models

# Create your models here.



class UserInfo(models.Model):

    name = models.CharField(verbose_name="姓名",max_length=16)
    password = models.CharField(verbose_name="密码", max_length=64)
    usertype_choice = (
        (1, '超级管理员'),
        (2, '管理员'),
        (3, '消费者'),
        (4, '供应商'),
        (5, '客服')
    )
    usertype = models.SmallIntegerField(verbose_name='用户类型', choices=usertype_choice, default=3)

    phone_number = models.CharField(verbose_name='手机号码',max_length=11,null=True,blank=True)
    wx_number = models.CharField(verbose_name='微信号码',max_length=18,null=True,blank=True)
    qq_number = models.CharField(verbose_name='QQ号码',max_length=10,null=True,blank=True)

    account = models.DecimalField(verbose_name="账户余额",max_digits=10,decimal_places=2,default=0)
    create_time = models.DateTimeField(verbose_name="账户创建时间",auto_now_add=True,null=True,blank=True)
    create_person = models.CharField(verbose_name="账户创建人",max_length=16)


class GameTokens(models.Model):
    name = models.CharField(verbose_name="姓名", max_length=16, default='默认用户名')
    game_name_choice = (
        (1,'王者荣耀'),
        (2,'和平精英'),
        (3,'英雄联盟手游'),
    )
    game_name = models.SmallIntegerField(verbose_name='游戏名称',choices=game_name_choice,default=1)


    denomination = models.SmallIntegerField(verbose_name='面额大小')
    charge_link = models.CharField(verbose_name='充值链接',max_length=256)

    order_create_time = models.DateTimeField(verbose_name='订单创建时间',auto_now_add=True)
    consumer = models.CharField(verbose_name='消费者',max_length=32)
    in_storage_person = models.CharField(verbose_name='入库人',max_length=128)
    order_number = models.CharField(max_length=128,unique=True)


class PhoneCharge(models.Model):
    name = models.CharField(verbose_name="姓名", max_length=16, default='默认用户名')
    operator_choice = (
        (1,'中国移动'),
        (2,'中国电信'),
        (3,'中国联通'),
    )
    operator = models.SmallIntegerField(verbose_name='运营商',choices=operator_choice,default=1)
    denomination = models.SmallIntegerField(verbose_name='面额大小')
    phonenumber = models.SmallIntegerField(verbose_name='手机号码')
    order_create_time = models.DateTimeField(verbose_name='订单创建时间',auto_now_add=True)
    consumer = models.CharField(verbose_name='消费者', max_length=32)
    in_storage_person = models.CharField(verbose_name='入库人', max_length=128)
    order_number = models.CharField(max_length=128, unique=True)


class Out_PhoneCharge(models.Model):
    userinfo = models.ForeignKey(UserInfo,on_delete=models.CASCADE,related_name='out_phonecharge',null=True, blank=True)
    name = models.CharField(verbose_name="姓名", max_length=16, default='默认用户名')
    operator_choice = (
        (1,'中国移动'),
        (2,'中国电信'),
        (3,'中国联通'),
    )
    operator = models.SmallIntegerField(verbose_name='运营商',choices=operator_choice,default=1)
    denomination = models.SmallIntegerField(verbose_name='面额大小')
    phonenumber = models.SmallIntegerField(verbose_name='手机号码')
    order_create_time = models.DateTimeField(verbose_name='订单创建时间')
    consumer = models.CharField(verbose_name='消费者', max_length=32)
    in_storage_person = models.CharField(verbose_name='入库人', max_length=128)
    order_number = models.CharField(max_length=128, unique=True)
    order_finish_time = models.DateTimeField(verbose_name='订单结束时间',auto_now_add=True)  # 新增
    order_status_choice = (
        (1, '已支付'),
        (2, '超时'),
        (3, '手动删除')
    )
    order_status = models.SmallIntegerField(verbose_name='订单状态', choices=order_status_choice, default=1)  # 新增
    out_storage_person = models.CharField(verbose_name='入库人', max_length=128)  # 新增
    vendor_denomination_phone = models.DecimalField(verbose_name="供货人此单账款",max_digits=10,decimal_places=2,default=0)

class Out_GameTokens(models.Model):
    userinfo = models.ForeignKey(UserInfo, on_delete=models.CASCADE, related_name='out_gametokens', null=True,blank=True)
    name = models.CharField(verbose_name="姓名", max_length=16, default='默认用户名')
    game_name_choice = (
        (1, '王者荣耀'),
        (2, '和平精英'),
        (3, '英雄联盟手游'),
    )
    game_name = models.SmallIntegerField(verbose_name='游戏名称', choices=game_name_choice, default=1)

    denomination = models.SmallIntegerField(verbose_name='面额大小')
    charge_link = models.CharField(verbose_name='充值链接', max_length=256)

    order_create_time = models.DateTimeField(verbose_name='订单创建时间')
    order_finish_time = models.DateTimeField(verbose_name='订单结束时间',auto_now_add=True)     #新增
    consumer = models.CharField(verbose_name='消费者', max_length=32)
    in_storage_person = models.CharField(verbose_name='入库人', max_length=128)
    order_number = models.CharField(max_length=128, unique=True)

    order_status_choice = (
        (1,'已支付'),
        (2,'超时'),
        (3,'手动删除')
    )
    order_status = models.SmallIntegerField(verbose_name='订单状态',choices=order_status_choice,default=1)  #新增
    out_storage_person = models.CharField(verbose_name='入库人', max_length=128)  #新增
    vendor_denomination_game = models.DecimalField(verbose_name="供货人此单账款", max_digits=10, decimal_places=2,default=0)


class Data(models.Model):
    name = models.CharField(verbose_name="姓名", max_length=16,default='默认用户名')

    #on_delete 是在外键字段中定义的一个参数，它控制当关联表的记录被删除时，当前表的记录应该如何处理
    #related_name 定义了反向访问的名称。通过 related_name，你可以从关联表中反向访问当前表的所有关联记录
#user = UserInfo.objects.get(id=1)  # 获取 UserInfo 表中的某条记录
# 通过 related_name 反向获取关联的 Data 表记录
# data_records = user.data.all()  # 返回与该 UserInfo 关联的所有 Data 记录
# print(data_records)
    userinfo = models.ForeignKey(UserInfo, on_delete=models.CASCADE, related_name='data',null=True, blank=True)
    out_phonecharge = models.ForeignKey(Out_PhoneCharge,on_delete=models.CASCADE,related_name='data',null=True,blank=True)
    out_gametokens = models.ForeignKey(Out_GameTokens,on_delete=models.CASCADE,related_name='data',null=True,blank=True)
    #account = models.CharField(verbose_name='账户余额',max_length=32,null=True,blank=True)
    total_consume = models.IntegerField(verbose_name='总消费金额',null=True,blank=True,default=0)
    total_all_fee = models.IntegerField(verbose_name='总流水金额',null=True,blank=True,default=0)
    total_all_order_num = models.IntegerField(verbose_name='总订单数',null=True,blank=True,default=0)
    total_phone_num = models.IntegerField(verbose_name='话费订单数',null=True,blank=True,default=0)
    total_game_num = models.IntegerField(verbose_name='话费订单数',null=True,blank=True,default=0)
    total_phone_fee = models.IntegerField(verbose_name='话费总金额',null=True,blank=True,default=0)
    total_game_fee = models.IntegerField(verbose_name='点券总金额',null=True,blank=True,default=0)
    system_fee = models.IntegerField(verbose_name='系统费',null=True,blank=True,default=0)
    vendor_currency = models.DecimalField(verbose_name="应结算账款",max_digits=10,decimal_places=2,default=0)


class Discount(models.Model):
    name = models.CharField(verbose_name='用户名',max_length=16,default='默认用户名')
    game_discount_for_consumer = models.DecimalField(verbose_name='消费者的点券折扣',max_digits=10,decimal_places=2,null=True,blank=True,default=1)
    phone_discount_for_consumer = models.DecimalField(verbose_name='消费者的话费折扣',max_digits=10,decimal_places=2, null=True, blank=True, default=1)
    game_discount_for_vendor = models.DecimalField(verbose_name='供货商的点券折扣', max_digits=10, decimal_places=2,null=True, blank=True, default=1)
    phone_discount_for_vendor = models.DecimalField(verbose_name='供货商的话费折扣', max_digits=10, decimal_places=2,null=True, blank=True, default=1)
    userinfo = models.ForeignKey(UserInfo,on_delete=models.CASCADE,related_name='discount',null=True,blank=True)



